package com.huilian.iotbox.client.controller;

import com.huilian.iotbox.client.security.jwt.util.JwtTokenUtil;
import com.huilian.iotbox.client.service.MemberService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * MemberController
 *
 * @Author mineChen
 * @Date 2021/12/18 下午 02:34
 */
@Slf4j
@RestController
@RequestMapping("/api/member")
public class MemberController extends BaseController {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private MemberService memberService;

    /**
     * 单独获取会员信息
     *
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping("/info")
    public ServerResponse info(HttpServletRequest request) throws Exception {
        final MemberDto memberFromReq = jwtTokenUtil.getMemberFromReq(request);
        return memberService.queryInfoById(memberFromReq);
    }

    /**
     * 我的订单数据
     */
    @GetMapping("/findMemberOrderList")
    public ServerResponse findMemberOrderList(HttpServletRequest request, CommonOrderDto commonOrderDto) throws Exception {
        final MemberDto memberFromReq = jwtTokenUtil.getMemberFromReq(request);
        commonOrderDto.setMemberId(memberFromReq.getId());
        return memberService.findMemberOrderList(commonOrderDto);
    }

    /**
     * 查询单个订单详情
     */
    @GetMapping("/findMemberOrderOne")
    public ServerResponse findMemberOrderOne(HttpServletRequest request, CommonOrderDto commonOrderDto) throws Exception {
        final MemberDto memberFromReq = jwtTokenUtil.getMemberFromReq(request);
        commonOrderDto.setMemberId(memberFromReq.getId());
        return memberService.findMemberOrderOne(commonOrderDto);
    }



    /**
     * 支付宝小程序登录
     */
    @ResponseBody
    @PostMapping("/aliLogin")
    public ServerResponse aliLogin(HttpServletRequest request, @RequestBody AliUserDto aliUserDto) throws Exception {

        return memberService.aliLogin(aliUserDto);
    }


    /**
     * 微信支付宝小程序获取用户手机号
     *
     * @param memberDto
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/applet/getPhoneNumber")
    public ServerResponse getPhoneNumber(@RequestBody MemberDto memberDto, HttpServletRequest request) throws Exception {

        final MemberDto memberFromReq = jwtTokenUtil.getMemberFromReq(request);
        memberDto.setId(memberFromReq.getId());
        return memberService.getPhoneNumber(memberDto);
    }

    /**
     * 生成会员核销码
     */
    @PostMapping("/getCheckCodeQr")
    public ServerResponse getCheckCodeQr(@RequestBody MemberDto memberDto, HttpServletRequest request) throws Exception {
        final MemberDto memberFromReq = jwtTokenUtil.getMemberFromReq(request);
        memberFromReq.setLanguage(memberDto.getLanguage());
        return memberService.getEncryptCheckCode(memberFromReq, request);
    }

    @PostMapping("/updateInfo")
    public ServerResponse updateInfo(HttpServletRequest request, @RequestBody MemberDto member) throws Exception {
        MemberDto memberDto = jwtTokenUtil.getMemberFromReq(request);
        member.setId(memberDto.getId());
        return memberService.updateInfo(member);
    }

    @PostMapping("/noticeAppLoginSuc")
    public ServerResponse noticeAppLoginSuc(HttpServletRequest request, @RequestBody MemberDto member) throws Exception {
        MemberDto memberDto = jwtTokenUtil.getMemberFromReq(request);
        member.setId(memberDto.getId());
        return memberService.noticeAppLoginSuc(member);
    }


    @GetMapping("/findBasicInformationById")
    public ServerResponse findBasicInformationById(HttpServletRequest request) throws Exception {
        MemberDto memberDto = jwtTokenUtil.getMemberFromReq(request);
        return ServerResponse.createBySuccess(memberService.findBasicInformationById(memberDto.getId()));
    }
    @GetMapping("/findUnionIdThereById")
    public ServerResponse findUnionIdThereById(MemberDto memberDto, HttpServletRequest request) throws Exception {
        final MemberDto memberFromReq = jwtTokenUtil.getMemberFromReq(request);
        memberDto.setId(memberFromReq.getId());
        return memberService.findUnionIdThereById(memberDto);
    }

    /**
     * 用户同意《个人信息保护政策》、《用户服务协议》
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/acquiredInfo")
    public ServerResponse acquiredInfo(HttpServletRequest request) throws Exception {
        final MemberDto memberFromReq = jwtTokenUtil.getMemberFromReq(request);
        return memberService.acquiredInfo(memberFromReq);
    }

}
